﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Reflection;
using System.Data;
using System.IO;
using System.Data.SQLite;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.Net;
using Newtonsoft.Json.Linq;

namespace cadbom
{
    class NetApi
    {
        private string domain;
        private SqlLiteUtils sqlLite;

        public void getUserInfo()
        {
            try
            {
                sqlLite = new SqlLiteUtils();
                //sqlLite.createDb();
                //String password = StringSecurity.DESEncrypt("123456");
                //sqlLite.insertData("admin", password);
                //String encode = StringSecurity.DESDecrypt(password);
                string sql = "select account,password,domainid,token from user";
                SQLiteDBHelper db = new SQLiteDBHelper(GValues.dbpath);
                using (SQLiteDataReader reader = db.ExecuteReader(sql, null))
                {
                    while (reader.Read())
                    {
                        //editor.WriteMessage(reader.GetString(0) + " " + reader.GetString(1) + "\n");
                        //Console.WriteLine("account:{0},password{1}", reader.GetString(0), reader.GetString(1));
                        GValues.account = reader.GetString(0);
                        GValues.password = StringSecurity.DESDecrypt(reader.GetString(1));
                        GValues.tenant = reader.GetString(2);
                        GValues.token = reader.GetString(3);
                    }
                }
            }
            catch (System.Exception e)
            {
                //editor.WriteMessage(e.ToString());
            }
        }
        public void InitDomainConfig()
        {
            // 指定配置文件的路径
            string configFilePath = GValues.configPath; //"E:\\Program Files\\Autodesk\\AutoCAD 2014\\erptestdll\\config.txt";
            try
            {
                // 读取文本文件的所有行
                string[] lines = File.ReadAllLines(configFilePath);

                // 遍历每一行并处理配置信息
                foreach (string line in lines)
                {
                    // 假设配置文件的每一行都是以"键=值"的格式存储
                    string[] keyValue = line.Split('=');
                    if (keyValue.Length == 2)
                    {
                        string key = keyValue[0].Trim();
                        string value = keyValue[1].Trim();
                        // 在这里处理配置信息，例如存储到字典或使用
                        if (key == "domain")
                        {
                            domain = value;
                        }
                        try
                        {
                            if (key == "domainLink")
                            {
                                GValues.domainLink = value;
                            }
                        }
                        catch (System.Exception e)
                        {
                            //editor.WriteMessage(e.ToString());
                        }
                    }
                }
            }
            catch (IOException e)
            {
                //editor.WriteMessage("读取配置文件时发生错误: " + e.Message);
            }

        }
                public List<T> ConvertDataTableToList<T>(System.Data.DataTable dataTable) where T : new()
        {
            List<T> list = new List<T>();

            foreach (DataRow row in dataTable.Rows)
            {
                T obj = new T();

                foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
                {
                    if (dataTable.Columns.Contains(propertyInfo.Name))
                    {
                        if (row[propertyInfo.Name] != DBNull.Value)
                        {
                            propertyInfo.SetValue(obj, row[propertyInfo.Name], null);
                        }
                    }
                }

                list.Add(obj);
            }

            return list;
        }

                public void saveProcessBom(List<Dictionary<String, Dictionary<String, String>>> productProcessList, Action<String> callback)
                {
                    string postData = JsonConvert.SerializeObject(productProcessList);
                    Http.CreateHttpClient(GValues.domainLink + "mrp/mrpProcessBom/cad-updates", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r1) =>
                    {
                        var responCode = Convert.ToInt32(r1.Code);
                        if (responCode == 200) //访问成功
                        {
                            callback.Invoke("ok");
                        }
                        else
                        {
                            //MessageBox.Show("保存产品工艺BOM数据失败！" + r1.Code + " " + " " + responCode + " " + r1.Error);
                        }
                    }));
                }

                public void saveProductList(List<BasProductImport> importProductList, Action<List<BasProduct>> callback)
                {
                    String tag = "saveProductList";
                    string postData = JsonConvert.SerializeObject(importProductList);
                    Http.CreateHttpClient(GValues.domainLink + "/bas/basProduct/importByExcelBOM", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
                    {
                        var responCode = Convert.ToInt32(r.Code);
                        if (responCode == 200) //访问成功
                        {
                            //ResponResult<Object> cc = JsonConvert.DeserializeObject<ResponResult<Object>>(r.AckStr());
                            ResponResult<BindingList<BasProduct>> cc = JsonConvert.DeserializeObject<ResponResult<BindingList<BasProduct>>>(r.AckStr());
                            if (cc.success)
                            {
                                BindingList<BasProduct> productList = cc.result;
                                callback.Invoke(productList.ToList());
                                LogHelper.WriteLog(tag + "保存产品数据成功！");
                            }
                            else
                            {
                                callback.Invoke(null);
                                LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                            }


                        }
                        else
                        {
                            LogHelper.WriteLog(tag + "保存产品数据失败！" + r.Code + " " + " " + responCode + " " + r.Error);

                        }
                    }));
                }

                public void updateParentProductBom(String parentProductCode, String childProductCode,int cQty,Action<String> callback)
                {
                    String tag = "updateParentProductBom";
                    if (parentProductCode != null && childProductCode != null && parentProductCode != "" && childProductCode != "") {
                        getProductData(parentProductCode, new Action<BasProduct>((product) =>
                        {
                            if (product != null)
                            {
                                getProductData(childProductCode, new Action<BasProduct>((cProduct) =>
                                {
                                    if (cProduct != null)
                                    {
                                        getProductBom(product.id, new Action<List<ProductBomTree>>((boms) =>
                                        {
                                            if (boms != null && boms.Count > 0)
                                            {
                                                MrpProductBomVo mrpProductBomVo = new MrpProductBomVo();
                                                mrpProductBomVo.productBomHandleId = GValues.cadHandleId;
                                                mrpProductBomVo.pid = product.id;
                                                List<MrpProductBom> mrpProductBoms = new List<MrpProductBom>();
                                                MrpProductBom mrpProductBom = null;
                                                int i = 0;
                                                foreach (ProductBomTree item in boms)
                                                {
                                                    mrpProductBom = new MrpProductBom();
                                                    mrpProductBom.nodeId = item.id;
                                                    mrpProductBom.pid = item.pid;
                                                    mrpProductBom.qty = item.qty;
                                                    mrpProductBom.hasChild = item.nodeType == "2" ? "true" : "false";
                                                    mrpProductBom.productionMethod = item.nodeType == "2" ? "1" : "2";
                                                    mrpProductBom.nodeType = item.nodeType;
                                                    mrpProductBom.unitId = item.unitId;
                                                    mrpProductBom.handleId = item.handleId;
                                                    mrpProductBom.sort = item.sort;
                                                    mrpProductBoms.Add(mrpProductBom);
                                                }
                                                int sort = boms.Count;
                                                sort++;
                                                mrpProductBom = new MrpProductBom();
                                                mrpProductBom.nodeId = cProduct.id;
                                                mrpProductBom.pid = product.id;
                                                mrpProductBom.qty = cQty.ToString();
                                                mrpProductBom.hasChild = "true";
                                                mrpProductBom.productionMethod = "1";
                                                mrpProductBom.nodeType = "2";
                                                mrpProductBom.unitId = cProduct.unitId;
                                                mrpProductBom.handleId = "";
                                                mrpProductBom.sort =sort.ToString();
                                                mrpProductBoms.Add(mrpProductBom);
                                                mrpProductBomVo.mrpProductBoms = mrpProductBoms;
                                                saveProductBom(mrpProductBomVo, new Action<string>(res =>
                                                {
                                                    if (res == "ok") //访问成功
                                                    {
                                                        MessageBox.Show("更新父产品BOM物料数据成功！");
                                                        LogHelper.WriteLog(tag + "更新父产品BOM物料数据成功！");
                                                    }
                                                    else
                                                    {
                                                        LogHelper.WriteLog(tag + "更新父产品BOM物料数据失败！");

                                                    }
                                                }));

                                            }
                                            else
                                            {
                                                callback.Invoke(null);
                                                LogHelper.WriteLog(tag + "更新产品BOM物料数据失败！");
                                            }

                                        }));
                                    }
                                }));
                            }
                        }));
                    } 
                }

                public void saveSemiProductBom(CadProductBom bomVo, Action<String> callback)
                {
                    String tag = "saveSemiProductBom";
                    string postData = JsonConvert.SerializeObject(bomVo);
                    Http.CreateHttpClient(GValues.domainLink + "/mrp/mrpProductBom/add-semiproduct", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
                    {
                        var responCode = Convert.ToInt32(r.Code);
                        if (responCode == 200) //访问成功
                        {
                            ResponResult<Object> cc = JsonConvert.DeserializeObject<ResponResult<Object>>(r.AckStr());
                            if (cc.success)
                            {
                                callback.Invoke("ok");
                                LogHelper.WriteLog(tag + "保存半成品产品BOM物料数据成功！");
                            }
                            else
                            {
                                callback.Invoke("fail");
                                LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                            }


                        }
                        else
                        {
                            LogHelper.WriteLog(tag + "保存半成品产品BOM物料数据失败！" + r.Code + " " + " " + responCode + " " + r.Error);

                        }
                    }));
                }
        public void saveProductBom(MrpProductBomVo mrpProductBomVo,Action<String> callback)
        {
            String tag = "saveProductBom";
            string postData = JsonConvert.SerializeObject(mrpProductBomVo);
            Http.CreateHttpClient(GValues.domainLink + "/mrp/mrpProductBom/add-material", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
            {
                var responCode = Convert.ToInt32(r.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<Object> cc = JsonConvert.DeserializeObject<ResponResult<Object>>(r.AckStr());
                    if (cc.success)
                    {
                        callback.Invoke("ok");
                        LogHelper.WriteLog(tag + "保存产品BOM物料数据成功！");
                    }
                    else
                    {
                        callback.Invoke("fail");
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                  
                   
                }
                else
                {
                    LogHelper.WriteLog(tag + "保存产品BOM物料数据失败！" + r.Code + " " + " " + responCode + " " + r.Error);

                }
            }));
        }
        public void importByExcel(List<BasProductImport> importProductList, Action<string> callback)
        {
            String tag = "saveProductList";
            string postData = JsonConvert.SerializeObject(importProductList);
            Http.CreateHttpClient(GValues.domainLink + "/bas/basProduct/importByExcel", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
            {
                var responCode = Convert.ToInt32(r.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<Object> cc1 = JsonConvert.DeserializeObject<ResponResult<Object>>(r.AckStr());
                    ResponResult<BindingList<BasProduct>> cc = JsonConvert.DeserializeObject<ResponResult<BindingList<BasProduct>>>(r.AckStr());
                    if (cc.success && cc.message.IndexOf("导入成功数据:1") !=-1)
                    {
                       
                        callback.Invoke("ok");
                        LogHelper.WriteLog(tag + "保存产品数据成功！");
                    }
                    else
                    {
                        callback.Invoke(null);
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }


                }
                else
                {
                    LogHelper.WriteLog(tag + "保存产品数据失败！" + r.Code + " " + " " + responCode + " " + r.Error);

                }
            }));
        }


        public void saveBasMaterial(BasMaterial basMaterial, Action<BasMaterial> callback)
        {
            String tag = "saveBasMaterial";
            string postData = JsonConvert.SerializeObject(basMaterial);
            Http.CreateHttpClient(GValues.domainLink + "/bas/basMaterial/add", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
            {
                var responCode = Convert.ToInt32(r.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<Object> cc1 = JsonConvert.DeserializeObject<ResponResult<Object>>(r.AckStr());
                    ResponResult<BasMaterial> cc = JsonConvert.DeserializeObject<ResponResult<BasMaterial>>(r.AckStr());
                    if (cc1.success)
                    {
                        callback.Invoke(cc.result);
                        LogHelper.WriteLog(tag + "保存物料数据成功！");
                    }
                    else
                    {
                        callback.Invoke(null);
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }


                }
                else
                {
                    LogHelper.WriteLog(tag + "保存产品数据失败！" + r.Code + " " + " " + responCode + " " + r.Error);

                }
            }));
        }
        public void getMaterialCode(string typeCode, Action<string> callback)
        {
            String tag = "getMaterialCode";
            string postData = "{}";
            Http.CreateHttpClient(GValues.domainLink + "/sys/fillRule/executeRuleByCode/" + typeCode, GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
            {
                var responCode = Convert.ToInt32(r.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<Object> cc1 = JsonConvert.DeserializeObject<ResponResult<Object>>(r.AckStr());
                    if (cc1.success && cc1.result !=null)
                    {
                        string code = cc1.result.ToString();
                        string[] codes = code.Split('-');
                        callback.Invoke(codes[1]);
                    }
                    else
                    {
                        callback.Invoke(null);
                    }


                }
                else
                {
                    LogHelper.WriteLog(tag + "保存产品数据失败！" + r.Code + " " + " " + responCode + " " + r.Error);

                }
            }));
        }
        public void getProductBom(string productId,Action<List<ProductBomTree>> callback)
        {
            String tag = "getProductBom";
            //获取到了token
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "mrp/mrpProductBom/list-child?nodeId=" + productId, new Action<Http.HttpResp>((r2) =>
            {

                var responCode = Convert.ToInt32(r2.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<BindingList<ProductBomTree>> cc = JsonConvert.DeserializeObject<ResponResult<BindingList<ProductBomTree>>>(r2.AckStr());
                    if (cc.success)
                    {
                        BindingList<ProductBomTree> boms = cc.result;
                        if (boms != null && boms.Count() > 0)
                        {
                            boms.Sort(item => item.code);
                            callback.Invoke(boms.ToList());
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r2.Error);
                }
            }));
        }
        public void getMaterialData(String materialCode, Action<ResponResultA<BasMaterial>> callback, int pageNo = 1, int pageSize = 100)
        {
            String tag = "getMaterialData";
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basMaterial/list?queryName=" + materialCode + "&pageNo=" + pageNo + "&pageSize=" + pageSize, new Action<Http.HttpResp>((r11) =>
            {
                var responCode = Convert.ToInt32(r11.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResultA<BasMaterial> cc = JsonConvert.DeserializeObject<ResponResultA<BasMaterial>>(r11.AckStr());
                    if (cc.success)
                    {
                        BindingList<BasMaterial> products = cc.result.records;
                        if (products != null)
                        {                            
                            callback.Invoke(cc);        
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r11.Error);
                }
            }));
        }
        public void getSystemParamater(Action<ResponResultA<JObject>> callback, int pageNo = 1, int pageSize = 1000)
        {
            String tag = "getSystemParamater";
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basUserPrintBuffer/list?username=system&printModule=systemParameter&pageNo=" + pageNo + "&pageSize=" + pageSize, new Action<Http.HttpResp>((r11) =>
            {
                var responCode = Convert.ToInt32(r11.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResultA<JObject> cc = JsonConvert.DeserializeObject<ResponResultA<JObject>>(r11.AckStr());
                    if (cc.success)
                    {
                        BindingList<JObject> products = cc.result.records;
                        if (products != null)
                        {
                            callback.Invoke(cc);
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r11.Error);
                }
            }));
        }

        public void getMaterialCategory(Action<ResponResultA<BasMaterialCategory>> callback, int pageNo = 1, int pageSize = 1000)
        {
            String tag = "getMaterialCategory";
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basMaterialCategory/rootList?pageNo=" + pageNo + "&pageSize=" + pageSize, new Action<Http.HttpResp>((r11) =>
            {
                var responCode = Convert.ToInt32(r11.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResultA<BasMaterialCategory> cc = JsonConvert.DeserializeObject<ResponResultA<BasMaterialCategory>>(r11.AckStr());
                    if (cc.success)
                    {
                        BindingList<BasMaterialCategory> products = cc.result.records;
                        if (products != null)
                        {
                            callback.Invoke(cc);
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r11.Error);
                }
            }));
        }

        public void getBasMeasureUnit(Action<ResponResultA<BasMeasureUnit>> callback, int pageNo = 1, int pageSize = 1000)
        {
            String tag = "getBasMeasureUnit";
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basMeasureUnit/list?pageNo=" + pageNo + "&pageSize=" + pageSize, new Action<Http.HttpResp>((r11) =>
            {
                var responCode = Convert.ToInt32(r11.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResultA<BasMeasureUnit> cc = JsonConvert.DeserializeObject<ResponResultA<BasMeasureUnit>>(r11.AckStr());
                    if (cc.success)
                    {
                        BindingList<BasMeasureUnit> products = cc.result.records;
                        if (products != null)
                        {
                            callback.Invoke(cc);
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r11.Error);
                }
            }));
        }
        private QueryParamater getQueryParam(String code)
        {

            //var superArr = System.Net.WebUtility.UrlDecode("");
            QueryParamater param = new QueryParamater();
            param.superQueryMatchType = "and";
            List<ParamArr> superArrs = new List<ParamArr>();
            ParamArr paramArr = new ParamArr();
            paramArr.rule = "eq";
            paramArr.type = "string";
            paramArr.val = code;
            paramArr.field = "p.code";
            superArrs.Add(paramArr);
            param.superQueryParams = System.Net.WebUtility.UrlDecode(JsonConvert.SerializeObject(superArrs));
            return param;
        }

        public void getProductDataList(String productCode, Action<ResponResultA<BasProduct>> callback, int pageNo = 1, int pageSize = 100)
        {
            String tag = "getProductDataList";
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basProduct/list?queryName=" + productCode + "&pageNo=" + pageNo + "&pageSize=" + pageSize, new Action<Http.HttpResp>((r11) =>
            {
                var responCode = Convert.ToInt32(r11.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResultA<BasProduct> cc = JsonConvert.DeserializeObject<ResponResultA<BasProduct>>(r11.AckStr());
                    if (cc.success)
                    {
                        BindingList<BasProduct> products = cc.result.records;
                        BasProduct product = new BasProduct();
                        if (products != null )
                        {
                            callback.Invoke(cc);
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r11.Error);
                }
            }));
        }

        public void getMaterialDataList(BasMaterial basMaterial,Action<List<BasMaterial>> callback)
        {
            QueryParamater param = new QueryParamater();
            param.superQueryMatchType = "and";
            List<ParamArr> superArrs = new List<ParamArr>();
            if (basMaterial.realName != null && basMaterial.realName != "")
            {
                ParamArr paramArr = new ParamArr();
                paramArr.rule = "like";
                paramArr.type = "string";
                paramArr.val = basMaterial.realName;
                paramArr.field = "m.realName";
                superArrs.Add(paramArr);
            }
            if (basMaterial.model != null && basMaterial.model !="")
            {
                ParamArr paramArr = new ParamArr();
                paramArr.rule = "like";
                paramArr.type = "string";
                paramArr.val = basMaterial.model;
                paramArr.field = "m.model";
                superArrs.Add(paramArr);
            }
            if (basMaterial.description != null && basMaterial.description != "")
            {
                ParamArr paramArr = new ParamArr();
                paramArr.rule = "like";
                paramArr.type = "string";
                paramArr.val = basMaterial.description;
                paramArr.field = "m.description";
                superArrs.Add(paramArr);
            }
            if (basMaterial.custCode != null && basMaterial.custCode != "")
            {
                ParamArr paramArr = new ParamArr();
                paramArr.rule = "like";
                paramArr.type = "string";
                paramArr.val = basMaterial.custCode;
                paramArr.field = "m.custCode";
                superArrs.Add(paramArr);
            }
            param.superQueryParams = System.Net.WebUtility.UrlDecode(JsonConvert.SerializeObject(superArrs));
            LogHelper.WriteLog("开始查询物料！" + GValues.tenant + " token :" + GValues.token);
            //MessageBox.Show("tenant :" + GValues.tenant + " token :" + GValues.token + " productcode : " + productCode + " domainLink: " + GValues.domainLink);
            String tag = "getProductData";
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basMaterial/list?superQueryParams=" + param.superQueryParams + "&superQueryMatchType=" + param.superQueryMatchType, new Action<Http.HttpResp>((r11) =>
            {
                //MessageBox.Show(" r :" + r11.Code);
                int responCode = 0;
                try
                {
                    responCode = Convert.ToInt32(r11.Code);
                }
                catch (Exception ex)
                {

                }

                if (r11.Code == HttpStatusCode.OK || responCode == 200) //访问成功
                {
                    ResponResultA<BasMaterial> cc = JsonConvert.DeserializeObject<ResponResultA<BasMaterial>>(r11.AckStr());
                    if (cc.success)
                    {
                        //MessageBox.Show(" message : " + cc.message);
                        BindingList<BasMaterial> products = cc.result.records;
                        List<BasMaterial> list = products.ToList();
                        if (products != null )
                        {
                            callback.Invoke(list);
                         }
                        else
                        {
                            callback.Invoke(null);
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r11.Error);
                }
            }));
        }
    

        public void getProductData(String productCode,Action<BasProduct> callback)
        {
            string code = productCode;
            QueryParamater param = new QueryParamater();
            param.superQueryMatchType = "and";
            List<ParamArr> superArrs = new List<ParamArr>();
            ParamArr paramArr = new ParamArr();
            paramArr.rule = "eq";
            paramArr.type = "string";
            paramArr.val = code;
            paramArr.field = "p.code";
            superArrs.Add(paramArr);
            param.superQueryParams = System.Net.WebUtility.UrlDecode(JsonConvert.SerializeObject(superArrs));
            LogHelper.WriteLog("开始查询产品！" + GValues.tenant + " token :" + GValues.token);
            //MessageBox.Show("tenant :" + GValues.tenant + " token :" + GValues.token + " productcode : " + productCode + " domainLink: " + GValues.domainLink);
            String tag = "getProductData";
            Http.CreateHttpClient(GValues.tenant, GValues.token).GetAsync(GValues.domainLink + "bas/basProduct/list?superQueryParams=" + param.superQueryParams + "&superQueryMatchType=" + param.superQueryMatchType, new Action<Http.HttpResp>((r11) =>
            {
                //MessageBox.Show(" r :" + r11.Code);
                int responCode = 0;
                try
                {
                    responCode = Convert.ToInt32(r11.Code);
                }
                catch (Exception ex)
                {

                }

                if (r11.Code == HttpStatusCode.OK || responCode == 200) //访问成功
                {
                    ResponResultA<BasProduct> cc = JsonConvert.DeserializeObject<ResponResultA<BasProduct>>(r11.AckStr());
                    if (cc.success)
                    {
                        //MessageBox.Show(" message : " + cc.message);
                        BindingList<BasProduct> products = cc.result.records;
                        BasProduct product = new BasProduct();
                        if (products != null && products.Count() == 1)
                        {
                            product = products[0];
                            //MessageBox.Show("product: " + product.code);
                            if (product.code != null && productCode == product.code.Trim())
                            {
                                GValues.cadHandleId = product.cadHandleId;
                                GValues.productCode = product.code;
                                GValues.productCustCode = product.custCode;
                                callback.Invoke(product);
                            }
                            else
                            {
                                LogHelper.WriteLog(tag + "没找到产品数据！");
                            }
                        }
                        else
                        {
                            callback.Invoke(null);
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog(tag + "接口访问错误: " + cc.message);
                    }
                }
                else
                {
                    LogHelper.WriteLog(tag + "网络连接错误: " + r11.Error);
                }
            }));
        }
    }
}
